package com.chen.controller;

import com.alibaba.fastjson2.JSON;
import com.chen.annotation.Counter;
import com.chen.annotation.RateLimiter;
import com.chen.core.domain.AjaxResult;
import com.chen.core.domain.entity.SysUser;
import com.chen.domain.Document;
import com.chen.enums.LimitType;
import com.chen.service.valid.ValidAdd;
import com.chen.service.valid.ValidAll;
import com.chen.service.valid.ValidEdit;
import com.chen.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;

@Validated
@RestController
@Slf4j
public class ValidController {

    @Autowired
    protected HttpServletRequest httpServletRequest;


    @GetMapping("/valid")
    public void test(@Validated(value = {ValidEdit.class, ValidAll.class}) Document document, HttpServletResponse response) {
        String name = httpServletRequest.getHeader("name");
        log.info("document: {}", document);
        log.info("response: {}", response);
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success("测试成功")));
    }


    @GetMapping("/validRequestBody")
    public void validRequestBody(@Validated(value = {ValidEdit.class, ValidAll.class}) @RequestBody Document document, HttpServletResponse response) {
        String name = httpServletRequest.getHeader("name");
        log.info("document: {}", document);
        log.info("response: {}", response);
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success("测试成功")));
    }


    @Counter
    @GetMapping("/validPath/{age}")
    public void validRequestBody(@PathVariable @Min(value = 1,message = "最小值为1") Integer age, HttpServletResponse response) {
        log.info("age: {}", age);
        log.info("response: {}", response);
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.success("测试成功")));
    }
}
